package nl.fonfolio.view {
	import flash.display.*;
	import flash.text.TextField;

	import nl.fonfolio.model.Model;
	import nl.fonfolio.controller.Controller;
	import nl.fonfolio.core.FonfolioEvent;	

	import flash.events.MouseEvent;	
	import flash.events.*;	

	import nl.fonfolio.view.ProjectThumb;
	import nl.fonfolio.core.Fonfolio;	

	//import br.com.stimuli.loading.BulkLoader;	
	
	/**
	 * @author Niels
	 */
	public class ProfileProject extends Sprite {
		private var model : Model;
		private var controller : Controller;
		public var fonfolio : Fonfolio;
		public var projects_label : TextField;
		public var rss_btn : MovieClip;
		public var widget : Object;	
		public var widgetcontainer : Sprite;// = new Sprite();
		public var projectMask : Sprite = new Sprite();

		public function ProfileProject(fonfolio : Fonfolio,model : Model,controller : Controller) {
			this.fonfolio = fonfolio;
			this.model = model;
			this.controller = controller;
			this.widgetcontainer = new Sprite();
			this.projectMask = new Sprite();
					
			this.addChild(this.widgetcontainer);
			this.addChild(this.projectMask);
			
			this.model.addEventListener(FonfolioEvent.CONTROL_TYPE, this.update);
		}

		function drawMask(x : Number,y : Number, width : Number,height : Number) : void {	
			this.projectMask.graphics.clear();	
			this.projectMask.graphics.beginFill(0xFFFFFF, 1);	
			this.projectMask.graphics.drawRect(x, y, width, height);	
			this.projectMask.graphics.endFill();
		}

		private function update(event : FonfolioEvent) : void {
			switch (event.command) {
				case "LOAD_PROFILEPROJECT":
					var portfolio : Object = this.model.getProfileProject();
						
					//positioning
					for (var i : Number = 0;i <= portfolio.length - 1;i++) {
						var settings : Object = portfolio[i];
						var projectthumb : ProjectThumb = new ProjectThumb(this.fonfolio, this.model, this.controller, settings);	
						
						this.widgetcontainer.addChild(projectthumb);
						var c : Number = this.projects_label.y + this.projects_label.height;
						var d : Number = this.height - c;
						var f : Number = c + d / 2;
						this.widgetcontainer.y = f - this.widgetcontainer.height / 2 - 2; 
						//positioned in center (calculated from bottom label to bottom profileproject (minus white line)) 
						this.widgetcontainer.x = this.projects_label.x;
						projectthumb.x = i * (projectthumb.width + 12);
					}
					drawMask(this.widgetcontainer.x, this.widgetcontainer.y, (projectthumb.width + 12) * 3 - 12, this.widgetcontainer.height);
					this.widgetcontainer.mask = this.projectMask;

					this.addEventListener(MouseEvent.MOUSE_OVER, this.controlEnterSlider); 
					this.addEventListener(MouseEvent.MOUSE_OUT, this.controlLeaveSlider); 
					
					
					
					break;
			}
		}

		private function controlEnterSlider(event : MouseEvent) : void {
			this.addEventListener(Event.ENTER_FRAME, this.controlSliderEnterFrame); 
		}

		public function controlLeaveSlider(event : Event) : void {
			this.removeEventListener(Event.ENTER_FRAME, this.controlSliderEnterFrame); 
		}

		public function controlSliderEnterFrame(evt : Event) : void {
			var rapporto : Number = this.widgetcontainer.width / this.projectMask.width;
			var arrX : Number = -mouseX * rapporto + this.projectMask.width / 2;
			var dx : Number = arrX - this.widgetcontainer.x;
			var ax : Number = dx * .1;
			this.widgetcontainer.x += ax / 2;
            
            
			var temp = 0 - (this.widgetcontainer.width - 20);
			if (this.widgetcontainer.x >= -400 || this.widgetcontainer.x <= 400){
            	
            } 
		}
	}
}
